import { createRouter, createWebHashHistory } from 'vue-router'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import(/* webpackChunkName: "home" */ '../views/home/Home')

  },
  {
    path: '/my',
    name: 'My',
    component: () => import(/* webpackChunkName: "my" */ '../views/My/my')

  },
  {
    path: '/cartList',
    name: 'CartList',
    component: () => import(/* webpackChunkName: "  cartList" */ '../views/CartList/CartList.vue')

  },
  {
    path: '/orderList',
    name: 'OrderList',
    component: () => import(/* webpackChunkName: "  orderList" */ '../views/OrderList/OrderList')

  },
  {
    path: '/orderComfirmation/:id',
    name: 'OrderComfirmation',
    component: () => import(/* webpackChunkName: "  orderComfirmation" */ '../views/orderComfirmation/orderComfirmation')

  },
  {
    path: '/shop/:id',
    name: 'Shop',
    component: () => import(/* webpackChunkName: "shop" */ '../views/shop/Shop')

  },
  {
    path: '/register',
    name: 'Register',
    component: () => import(/* webpackChunkName: "register" */ '../views/register/Register'),
    beforeEnter: (to, from, next) => {
      const {isLogin}=localStorage;
      isLogin ? next({name:'Home'}):next();
    }
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import(/* webpackChunkName: "login" */ '../views/login/Login'),
    beforeEnter: (to, from, next) => {
      const {isLogin}=localStorage;
      isLogin ? next({name:'Home'}):next();
    }
  },
  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to,from,next) =>{
    const {isLogin}=localStorage;
    ( !isLogin && (to.name !== "Login" && to.name!=='Register') ) ? next({name: 'Login'}) :next();
})

export default router
